﻿@using hc.epm.DataModel.Business;
@using hc.Plat.Common.Extend;
@model List<Epm_ProjectDataSubmit>
@{
    ViewBag.Title = "项目资料";
}
<style>
    .tab > div label {
        text-align: left !important;
        width: auto !important;
    }

    .tab .fileList, .tab .fileListImg {
        padding-right: 40px;
        margin: 30px 20px 20px 60px;
        font-size: 14px;
    }

    .fileList .fileShow {
        width: 100%;
    }

    .tab .fileList li {
        display: flex;
        list-style: none;
        height: 35px;
        line-height: 30px;
    }

        .tab .fileList li > div:nth-child(1) {
            flex: none !important;
            width: 350px !important;
            white-space: nowrap !important;
            overflow: hidden !important;
            text-overflow: ellipsis !important;
        }

        .tab .fileList li > div:nth-child(2) {
            flex: none !important;
            width: 60px !important;
        }
</style>
<div class="contentData">
    <ul class="tab-list">
        <li id="Edit">基本信息</li>
        <li id="ApprovalConstitute">总批复构成</li>
        <li id="ProjectISP">工程服务商</li>
        <li id="ProjectMainPoint">工程要点</li>
        <li id="MilestonePlan">工程节点</li>
        <li id="TimeManage">工期管理</li>
        @*<li id="ProjectData" class="active">项目资料</li>*@
        <li id="IsCrossingsManage">外部手续</li>
    </ul>
    <form id="mainForm">
        <div class="tab">
            @if (Model != null && Model.Count() > 0)
            {
                foreach (var item in Model)
                {
                    <div class="upload" style="margin-bottom:0px;">
                        <div><label>&nbsp;@item.FileName&nbsp;:</label></div>
                        <div class="btn-upload">
                            <input type="button" id="btnUploadFile" class="btnUploadFile " style="position: relative; z-index: 1;" value="上传附件" dataid="@item.SId" data-module="ConProject" data-right="SaveProjectData" />
                        </div>
                    </div>
                    <div style="width:100%;" id="@("fileList"+item.SId)" class="fileList">
                        <ul class="fileShow"></ul>
                    </div>
                }
            }
            <div class="button-group btn-center">
                <button type="button" class="saveBut " data-module="ConProject" data-right="SaveProjectData">保存</button>
                <input type="file" id="loadFile" style="display:none" onchange="uploadFile()" dataid="" />
                <input type="hidden" name="fileDataJson" id="fileDataJson" />
                <input type="hidden" name="ProjectId" value="@ViewBag.ProjectId" />
            </div>
        </div>
    </form>
</div>
<script src="~/Resource/plugins/layui-2.1.2/lay/modules/layer.js"></script>
<script>
    //获取附件
    $.post("/Files/GetFileListByTableIds", { tableName: 'Epm_ProjectDataSubmit', ids: '@ViewBag.TableIds' }, function (data) {
        if (data.Flag) {
            if (data.Data.length > 0) {
                data.Data.forEach(function (item, index, arr) {
                    if (item.ImageType == null) {
                        var odiv = '<li id="' + item.GuidId + '">';
                        odiv += '<div class="downloadFile"><img src="' + getFileImg(item.Name) + '" style="width: 20px;height: 20px;cursor: pointer;" class="imgShow"/><a href="javascript:;" target="_blank">' + item.Name + '</a></div>';
                        odiv += '<div>' + item.Size + '</div>';
                        odiv += '<span style="margin-left:20px;">' + item.CreateUserName + '</span>';
                        odiv += '<span style="margin-left:20px;">' + new Date(formatDateByJson(item.CreateTime)).Format("yyyy-MM-dd") + '</span>';
                        odiv += '<span style="margin-left:30px;cursor: pointer;color:blue" class="fileDel fileDelone" data-val="' + item.GuidId + '">删除</span>'
                        odiv += '</li>';
                        $('#fileList' + item.SId + ' .fileShow').append(odiv);
                    }

                    var obj = {};
                    obj.Name = item.Name;
                    obj.GuidId = item.GuidId;
                    obj.Size = item.Size;
                    obj.Group = item.Group;
                    obj.Url = item.Url;
                    obj.ImageType = item.ImageType;
                    obj.TableId = item.SId;
                    fileDataArray.push(obj);

                });
                var fileDataJson = JSON.stringify(fileDataArray);
                $("#fileDataJson").val(fileDataJson);
            }
        } else {
            parent.layer.alert(data.Message, { icon: 2 });
        }
    });
    var fileDataArray = [];
    function uploadFile() {
        if ($("#loadFile")[0].files.length == 0) {
            $("#loadFile").val("");
            return;
        }
        var fileObj = $("#loadFile")[0].files[0]; // js 获取文件对象
        var formFile = new FormData();
        formFile.append("file", fileObj);
        var path = formFile;

        $.ajax({
            url: "/Upload/UploadHB",
            type: "POST",
            data: path,
            contentType: false,
            processData: false,
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    var obj = {};
                    obj.Name = data[i].Name;
                    obj.GuidId = data[i].GuidId;
                    obj.Size = data[i].Size;
                    obj.ImageType = data[i].ImageType;
                    obj.Group = data[i].ResponseObject.FDFS_GROUP;
                    obj.Url = data[i].ResponseObject.FDFS_NAME;
                    obj.TableId = $("#loadFile").attr("dataid");
                    fileDataArray.push(obj);

                    if (data[i].ImageType == null) {
                        var imgUrl = getFileImg(obj.Name);
                        var odiv = '<li id="' + obj.GuidId + '">';
                        odiv += '<div><img src="' + imgUrl + '" style="width: 20px;height: 20px;cursor: pointer;" class="imgShow"/><a href="javascript:;" target="_blank">' + obj.Name + '</a></div>';
                        odiv += '<div style="text-align:right; width:80px;">' + obj.Size + '</div>';
                        odiv += '<span style="margin-left:20px;">' + data[i].UploadName + '</span>';
                        odiv += '<span style="margin-left:20px;">' + new Date(formatDateByJson(data[i].UploadDate)).Format("yyyy-MM-dd") + '</span>';
                        odiv += '<span style="margin-left:20px;cursor: pointer;color:blue" class="fileDel fileDelone" data-val="' + obj.GuidId + '">删除</span>'
                        odiv += '</li>';
                        $('#fileList' + $("#loadFile").attr("dataid") + ' .fileShow').append(odiv);
                    }
                }
                fileDataJson = JSON.stringify(fileDataArray);
                $("#fileDataJson").val(fileDataJson);
            }
        });
        $("#loadFile").val("");
    }

    $(function () {
        $(".btnUploadFile").click(function () {
            var dataid = $(this).attr("dataid");
            var dataprojectid = $(this).attr("dataprojectid");
            var datafileid = $(this).attr("datafileid");

            $("#loadFile").attr("dataid", dataid);
            $("#loadFile").attr("dataprojectid", dataprojectid);
            $("#loadFile").attr("datafileid", datafileid);
            $("#loadFile").click();
        });

        $("#Edit").click(function () {
            window.location.href = 'Edit?projectId=@ViewBag.ProjectId';
        });
        $("#ApprovalConstitute").click(function () {
            window.location.href = 'ApprovalConstitute?projectId=@ViewBag.ProjectId';
        });
        $("#ProjectMainPoint").click(function () {
            window.location.href = 'ProjectMainPoint?projectId=@ViewBag.ProjectId';
        });
        $("#MilestonePlan").click(function () {
            window.location.href = 'MilestonePlan?projectId=@ViewBag.ProjectId';
        });
        $("#ProjectISP").click(function () {
            window.location.href = 'ProjectISP?projectId=@ViewBag.ProjectId';
        });
        $("#TimeManage").click(function () {
            window.location.href = 'TimeManage?projectId=@ViewBag.ProjectId';
        });
        $("#IsCrossingsManage").click(function () {
            window.location.href = 'CrossingsManage?projectId=@ViewBag.ProjectId';
        });
        //数据提交
        $(".saveBut").click(function () {
            var search = $("form").serialize();
            $.post("ProjectData", search, function (data) {
                if (data.Flag) {
                    layer.msg("操作成功", { time: 1000, icon: 1 }, function () {
                        window.location.reload();
                    });
                } else {
                    layer.alert(data.Message, { icon: 2 });
                }
            });
        });

        //删除附件
        $("div[id^='fileList']").on("click", ".fileDelone", function () {
            $(this).parents("li").remove();
            var toremove = '';
            var id = $(this).data("val");
            //----删除初始文件对应的值
            fileDataArray = fileDataArray.filter(function (item, index, arr) {
                return item.GuidId != id
            })
            var fileDataJson = JSON.stringify(fileDataArray);
            $("#fileDataJson").val(fileDataJson);
        });
    });
</script>
